package database;

import java.util.*;

import sharedspace.*;
/**
 * control class that work with questions
 * @author G3
 *
 */
public class ManageQuestion {
	
	private Question question = null;
	
	
	public ManageQuestion(Question question)
	{
		this.question = question;
		
	}
	
	/**
	 * add new question to database 
	 */
	public void addQuestion()
	{
		QuestionControl questionControl = new QuestionControl();
		List<Question> list = questionControl.getQuestionListBySubjectID(this.question.getQuestionSubject());
		if (list.size() == 0)
		{
			question.setQuestionID(question.getQuestionSubject()+"001");
			JDBC.insertStatment("INSERT into question "+
					"value('"+question.getQuestionID()+"','"+question.getQuestionSubject()+"','"+question.getQuestion()+"','"+question.getAnswers()[0]+
					"','"+question.getAnswers()[1]+"','"+question.getAnswers()[2]+"','"+question.getAnswers()[3]+"',"+question.getCorrectAnswer()+",'"+
					question.getAuthor().getUserID()+"',"+Boolean.toString(question.isInUse())+",'"+question.getQuestionCommet()+"');");
			return;
			
		}
		List<String> stringsList = new ArrayList<String>();
		for (int i = 0; i<list.size(); i++)
			stringsList.add(list.get(i).getQuestionID());
		Collections.sort(stringsList);
		Integer questionID = stringsList.size();
		String str = stringsList.get(questionID-1);
		str = str.substring(2);
		questionID = Integer.parseInt(str);
		questionID++;
		str = questionID.toString();
		int length = str.length();
		switch (length)
		{
			case 1: String newst = new String("00");
			        str = newst+str;
			        break;
			case 2: newst = new String("0");
					str = newst+str;
					break;
			case 3: 
		
		}
		str = question.getQuestionSubject()+str;
		question.setQuestionID(str);
		JDBC.insertStatment("INSERT into question "+
							"value('"+question.getQuestionID()+"','"+question.getQuestionSubject()+"','"+question.getQuestion()+"','"+question.getAnswers()[0]+
							"','"+question.getAnswers()[1]+"','"+question.getAnswers()[2]+"','"+question.getAnswers()[3]+"',"+question.getCorrectAnswer()+",'"+
							question.getAuthor().getUserID()+"',"+Boolean.toString(question.isInUse())+",'"+question.getQuestionCommet()+"');");
				
		
	}
	
	public Question getQuestion()
	{
		return this.question;
	}

}

